package com.accesshollywood.grfxOrdering.orderTreeSpecial;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractCellEditor;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

import com.accesshollywood.SQL.GrfxDateTime;
import com.accesshollywood.grfxOrdering.basics.Colors;
import com.accesshollywood.grfxOrdering.basics.STATE;
import com.accesshollywood.grfxOrdering.orderTreeSpecial.models.SummaryTableModel;
import com.accesshollywood.indexables.User;
import com.accesshollywood.trace.Trace;

public class SummaryPanel extends JPanel {
	/*
	JButton grabButton;
	JTable mainTable;

	public static boolean flag_COUNTDOWN = true;

	public SummaryPanel(JTree tree, int row, SummaryTableModel model) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		mainTable = new JTable(model);
		for (int i = 0; i < SummaryTableModel.columnWidths.length; i++) {
			mainTable.getColumnModel().getColumn(i).setPreferredWidth(SummaryTableModel.columnWidths[i]);
		}
		if (tree.isExpanded(row)) {
			add(mainTable.getTableHeader(), BorderLayout.PAGE_START);
		}
		mainTable.setDefaultRenderer(Object.class, new SummaryRenderer());
		mainTable.addMouseListener(new RightClickListener());
		setBackgroundIfReady(mainTable, STATE.APPROVED);
		add(mainTable, BorderLayout.CENTER);
	}

	public SummaryPanel(SummaryTableModel model) {
		setLayout(new BorderLayout());
		mainTable = new JTable(model);
		for (int i = 0; i < SummaryTableModel.columnWidths.length; i++) {
			mainTable.getColumnModel().getColumn(i).setPreferredWidth(SummaryTableModel.columnWidths[i]);
		}

		mainTable.setDefaultRenderer(Object.class, new SummaryRenderer());
		mainTable.addMouseListener(new RightClickListener());
		setBackgroundIfReady(mainTable, STATE.WAITING_FOR_APPROVAL);
		add(mainTable, BorderLayout.CENTER);
	}

	public void setBackgroundIfReady(Component component, STATE state) {
		Color c = Colors.getColor(Colors.DEFAULT_EVEN);
		
		switch (state) {
		case WAITING_FOR_APPROVAL:
			c = Colors.getColor(Colors.WAITING_FOR_APPROVAL);
			break;
		case APPROVED:
			c = Colors.getColor(Colors.APPROVED);
			break;
		case COMPLETED:
			c = Colors.getColor(Colors.COMPLETED);
			break;
		case CLOSED:
			c = Colors.getColor(Colors.CLOSED);
			break;
		case ARCHIVED:
			c = Colors.getColor(Colors.ARCHIVED);
			break;
		}
		
		component.setBackground(c);
	}

	public class SummaryRenderer extends DefaultTableCellRenderer {

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

			SummaryTableModel model = (SummaryTableModel)table.getModel();
			STATE state = model.grfxOrder.getState();
			
			setBackgroundIfReady(c, state);
			switch (column) {
			case 1:
				SQLDate dueDate = (SQLDate) value;
				if (flag_COUNTDOWN) {
					this.setText(dueDate.toRemainingString());
				} else {
					this.setText(dueDate.toString());
				}
				if (dueDate.isPast()) {
					c.setBackground(Colors.getColor(Colors.OVERDUE));
				} else {
					setBackgroundIfReady(c, state);
				}
				this.setHorizontalAlignment(JLabel.RIGHT);

				break;
			case 4:
				User artist = (User) value;
				this.setText(artist.fullName());
			case 5:
				this.setHorizontalAlignment(JLabel.LEFT);
				break;
			default:
				this.setHorizontalAlignment(JLabel.CENTER);
				break;
			}
			return c;
		}
	}

	public class SummaryTableEditor extends AbstractCellEditor implements TableCellEditor {
		SummaryRenderer renderer = new SummaryRenderer();
		JTable table;

		public SummaryTableEditor() {
			this.table = table;
			//Trace.info("initing table editor");
		}

		@Override
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
			Component editor = renderer.getTableCellRendererComponent(table, value, isSelected, true, row, column);
			//Trace.info("editing the table...");
			switch (column) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				//Trace.info("-------------------------------------------------------------------------------------------------");
				DefaultComboBoxModel model = (DefaultComboBoxModel) value;
				JComboBox artistPicker = new JComboBox(model);
				// artistPicker.addActionListener(arg0);
				return artistPicker;
			case 5:
				break;
			}
			return editor;
		}

		@Override
		public Object getCellEditorValue() {
			//Trace.info("aoishdoahsdohasdoigas");
			return null;
		}
	}

	public class RightClickListener extends MouseAdapter {
		private void myPopupEvent(MouseEvent e) {
			int x = e.getX();
			int y = e.getY();
			
			JPopupMenu popup = new JPopupMenu();
			popup.add(new JMenuItem("GRAB"));
			popup.show(mainTable, x, y);
		}

		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger())
				myPopupEvent(e);
		}

		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger())
				myPopupEvent(e);
		}
	}

	public class SummaryMouseAdapter extends MouseAdapter {
		TreeAsTablesRendererMaster renderer;

		public SummaryMouseAdapter(TreeAsTablesRendererMaster renderer) {
			super();
			this.renderer = renderer;
		}

		public void mouseClicked(MouseEvent e) {
			int nRow = mainTable.rowAtPoint(e.getPoint());
			int nCol = mainTable.columnAtPoint(e.getPoint());

			Object objSel = mainTable.getValueAt(nRow, nCol);
			//Trace.info(nRow + "  " + nCol + "   " + objSel);

			switch (nCol) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				//Trace.info("-------------------------------------------------------------------------------------------------");
				SummaryTableEditor summaryTableEditor = new SummaryTableEditor();
				JComboBox c = (JComboBox) summaryTableEditor.getTableCellEditorComponent(mainTable, objSel, true, nRow, nCol);
				Rectangle cellBounds = mainTable.getCellRect(nRow, nCol, true);
				Rectangle newBounds = SwingUtilities.convertRectangle(renderer, cellBounds, renderer.getParent());
				//Trace.info(cellBounds + " " + newBounds);
				c.setBounds(newBounds);
				// renderer.removeAll();
				renderer.getParent().add(c);
				c.showPopup();
				break;
			case 5:
				break;
			}
		}
	}
	*/
}
